function JsonPost(url,data,cb){
  data.ajax = 1;
  if(wx.getStorageSync("userId") !== ''){
    data.userId = wx.getStorageSync("userId");
  }
  console.log(data);
  wx.request({
    url: "https://dk.9ok.xin/index.php/Api/" + url,
    data: data,
    method: 'POST',
    header:{
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      console.log(res);
      if (res.data.code == 200) {
        cb(res.data.data);
      } else {
        cb(false);
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }
  })
}

function isTelphone(mobile) {
  var myreg = /^((13|14|15|17|18)+\d{9})$/;
  if (!myreg.test(mobile)) {
    return false;
  } else {
    return true;
  }
}

function showLoading(title){
  if (!title){
    title = "数据加载中...请稍后...";
  }
  wx.showLoading({
    title: title,
  })
}

function closeLoading(){
  wx.hideLoading()
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 时间戳转化为年 月 日 时 分 秒
 * number: 传入时间戳
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致
*/
function formatTime(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

function htmldecode(str) {
  var s = "";
  if (str.length == 0) return "";
  s = str.replace(/&amp;/g, "&");
  s = s.replace(/&lt;/g, "<");
  s = s.replace(/&gt;/g, ">");
  s = s.replace(/&nbsp;/g, " ");
  s = s.replace(/&#39;/g, "\'");
  s = s.replace(/&quot;/g, "\"");

  s = s.replace(/\/upload\/ueditor/g, "https:\/\/dk.9ok.xin\/upload\/ueditor");
  return s;
}

module.exports = {
  JsonPost: JsonPost,
  isTelphone: isTelphone,
  showLoading: showLoading,
  closeLoading: closeLoading,
  formatTime: formatTime,
  htmldecode: htmldecode
}